<template>
    <page-meta :page-style="'overflow:' + (show ? 'hidden' : 'visible')"> </page-meta>
    <SkeletonCard v-if="skeletonLoading" />
    <view v-else class="tabbar_padding_b">
        <view class="carpage">
            <!-- 参与满减活动的商品 -->
            <view v-if="islogin && activityData.length">
                <view class="carpage_goods bg" v-for="(val, indexs) in activityData" :key="indexs">
                    <view class="carpage_head">
                        <view class="carpage_left">
                            <view class="logo">满减</view>
                            <view
                                class="saletips"
                                v-if="val[0].activity.activityType == 7 && !saleDetail[indexs].select">
                                <text>
                                    满{{ floatDivide(saleDetail[indexs].orderAmount, 100) }}元减{{
                                        floatDivide(saleDetail[indexs].reduce, 100)
                                    }}元
                                </text>
                            </view>
                            <view
                                class="saletips"
                                v-if="
                                    val[0].activity.activityType == 7 &&
                                    saleDetail[indexs].select &&
                                    !saleDetail[indexs].isMax
                                ">
                                <text>
                                    再凑<text class="color">
                                        {{
                                            floatDivide(saleDetail[indexs].orderAmount - saleDetail[indexs].money, 100)
                                        }}元
                                    </text>
                                    可减<text class="color">{{ floatDivide(saleDetail[indexs].reduce, 100) }}元</text>
                                </text>
                            </view>
                            <view
                                class="saletips"
                                v-if="
                                    val[0].activity.activityType == 7 &&
                                    saleDetail[indexs].select &&
                                    saleDetail[indexs].isMax
                                ">
                                <text>
                                    已满{{ floatDivide(saleDetail[indexs].orderAmount, 100) }}元，已减{{
                                        floatDivide(saleDetail[indexs].reduce, 100)
                                    }}元
                                </text>
                            </view>
                            <view
                                class="saletips"
                                v-if="val[0].activity.activityType == 8 && !saleDetail[indexs].select">
                                <text>
                                    满{{ saleDetail[indexs].orderAmount }}件打{{
                                        floatDivide(saleDetail[indexs].reduce, 10)
                                    }}折
                                </text>
                            </view>
                            <view
                                class="saletips"
                                v-if="
                                    val[0].activity.activityType == 8 &&
                                    saleDetail[indexs].select &&
                                    saleDetail[indexs].orderAmount > saleDetail[indexs].maxNum
                                ">
                                <text>
                                    再凑<text class="color"
                                        >{{ saleDetail[indexs].orderAmount - saleDetail[indexs].maxNum }}件</text
                                    >可打<text class="color"> {{ floatDivide(saleDetail[indexs].reduce, 10) }}折 </text>
                                </text>
                            </view>
                            <view
                                class="saletips"
                                v-if="
                                    val[0].activity.activityType == 8 &&
                                    saleDetail[indexs].select &&
                                    saleDetail[indexs].orderAmount <= saleDetail[indexs].maxNum
                                ">
                                <text>
                                    已满<text class="color">{{ saleDetail[indexs].orderAmount }}件</text>，已打<text
                                        class="color">
                                        {{ floatDivide(saleDetail[indexs].reduce, 10) }}折
                                    </text>
                                </text>
                            </view>
                        </view>
                        <view
                            class="saletips color"
                            @click="router.push('/pages-cart/activeDetail/index?activityId=' + saleDetail[indexs].id)">
                            <text
                                v-if="
                                    !saleDetail[indexs].select ||
                                    (val[0].activity.activityType == 7 && saleDetail[indexs].isMax) ||
                                    (val[0].activity.activityType == 8 &&
                                        saleDetail[indexs].orderAmount <= saleDetail[indexs].maxNum)
                                ">
                                查看活动
                            </text>
                            <text v-else>去凑单</text>
                            <img :src="getImgOssUrl('/ui-prod-2.5.0/right_1118.png')" class="detailPhoto" />
                        </view>
                    </view>
                    <view
                        v-for="(item, index) in val"
                        :key="index + item.id"
                        class="carpage_square"
                        @touchstart="itemTouchStart($event, index, 1, indexs)"
                        @touchend="itemTouchEnd($event, index, 1, indexs)"
                        @tap="goDetail(item)"
                        :style="{
                            transform:
                                manjianIndex === indexs && activeIndex === index && goodsType == 1 && itemTransLateX
                                    ? 'translateX( ' + itemTransLateX + 'rpx)'
                                    : 0,
                            transition: 'all 0.4s'
                        }">
                        <div
                            style="height: 200rpx; line-height: 200rpx"
                            @touchstart.stop="choose(index, indexs, 'manjian')"
                            @touchmove.stop
                            @touchend.stop>
                            <checkbox
                                value="item.checked"
                                :checked="item.checked"
                                style="transform: scale(0.7)"
                                color="#FE292B"
                                :disabled="item.activity && item.activity.activityType == 2"
                                class="checkbox round red" />
                        </div>
                        <view class="img_square">
                            <img
                                class="carpage_img"
                                :src="item.coverSrc || defaultImg"
                                :lazy-load="true"
                                @error="imageError(index)" />
                        </view>
                        <view class="carpage_detail">
                            <view class="carpage_foodname">{{ item.goodsName }}</view>
                            <view
                                v-if="item.onSale == 1 && item.skuStock && item.isDelete == 2"
                                class="carpage_stylechoose"
                                @touchmove.stop
                                @touchend.stop
                                @tap.stop="openDialog(item)">
                                <text class="carpage_text">{{ item.skuSpecValue }}</text>
                                <img class="carpage_jintou" :src="getImgOssUrl('/ui-prod-2.0.0/jiantou_1.2.png')" />
                            </view>
                            <view v-if="item.activity && item.activity.activityType == 2" class="activity_goods">
                                活动商品不支持购物车下单
                            </view>
                            <view v-else class="carpage_count">
                                <view class="carpage_color">
                                    <text class="price-qian">￥</text>
                                    <text class="carpage_money">
                                        {{
                                            floatDivide(item.skuPrice, 100).toString().indexOf('.') != -1
                                                ? floatDivide(item.skuPrice, 100).toString().split('.')[0]
                                                : floatDivide(item.skuPrice, 100)
                                        }}
                                    </text>
                                    <text
                                        class="price-dian"
                                        v-if="floatDivide(item.skuPrice, 100).toString().indexOf('.') != -1">
                                        {{ '.' + floatDivide(item.skuPrice, 100).toString().split('.')[1] }}
                                    </text>
                                </view>
                                <ShopingGoodsSteps
                                    @touchend.stop
                                    @AddShoppingList="AddShoppingList($event, index, indexs, 'manjian')"
                                    :num="item.quantity"
                                    :maxQuantity="item.skuStock" />
                            </view>
                        </view>
                        <view
                            v-if="manjianIndex === indexs && activeIndex === index && itemTransLateX && goodsType == 1"
                            @touchstart.stop
                            @touchmove.stop
                            @touchend.stop
                            class="del"
                            :style="{
                                top: 0,
                                right: itemTransLateX ? '-144rpx' : 0,
                                width: itemTransLateX ? '144rpx' : 0
                            }">
                            <view
                                class="delicon"
                                @touchmove.stop
                                @touchend.stop
                                @touchstart.stop="delGoods(item, index)">
                                <img class="iconimg" :src="getImgOssUrl('/ui-prod-2.0.0/delgoods_1.2.png')" />
                                <text class="icontext"> 删除 </text>
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <!-- 没参与满减活动的商品 -->
            <view class="carpage_goods bg" v-if="cartList.length && islogin">
                <view
                    v-for="(item, index) in cartList"
                    :key="index + item.id"
                    class="carpage_square"
                    @touchstart="itemTouchStart($event, index, 2)"
                    @touchend="itemTouchEnd($event, index, 2)"
                    @tap="goDetail(item)"
                    :style="{
                        transform:
                            activeIndex === index && goodsType == 2 && itemTransLateX
                                ? 'translateX( ' + itemTransLateX + 'rpx)'
                                : 0,
                        transition: 'all 0.4s'
                    }">
                    <div
                        style="height: 200rpx; line-height: 200rpx"
                        @touchstart.stop="choose(index)"
                        @touchmove.stop
                        @touchend.stop>
                        <checkbox
                            value="item.checked"
                            :checked="item.checked"
                            style="transform: scale(0.7)"
                            color="#FE292B"
                            :disabled="item.activity && item.activity.activityType == 2"
                            class="checkbox round red" />
                    </div>
                    <view class="img_square">
                        <img
                            class="carpage_img"
                            :src="item.coverSrc || defaultImg"
                            :lazy-load="true"
                            @error="imageError(index)" />
                    </view>
                    <view class="carpage_detail">
                        <view class="carpage_foodname">{{ item.goodsName }}</view>
                        <view class="carpage_stylechoose" @touchmove.stop @touchend.stop @tap.stop="openDialog(item)">
                            <text class="carpage_text">{{ item.skuSpecValue }}</text>
                            <img class="carpage_jintou" :src="getImgOssUrl('/ui-prod-2.0.0/jiantou_1.2.png')" />
                        </view>
                        <view v-if="item.activity && item.activity.activityType == 2" class="activity_goods">
                            活动商品不支持购物车下单
                        </view>
                        <view v-else class="carpage_count">
                            <view class="carpage_color">
                                <text class="price-qian">￥</text>
                                <text class="carpage_money">
                                    {{
                                        floatDivide(item.skuPrice, 100).toString().indexOf('.') != -1
                                            ? floatDivide(item.skuPrice, 100).toString().split('.')[0]
                                            : floatDivide(item.skuPrice, 100)
                                    }}
                                </text>
                                <text
                                    class="price-dian"
                                    v-if="floatDivide(item.skuPrice, 100).toString().indexOf('.') != -1">
                                    {{ '.' + floatDivide(item.skuPrice, 100).toString().split('.')[1] }}
                                </text>
                            </view>

                            <ShopingGoodsSteps
                                @touchend.stop
                                @AddShoppingList="AddShoppingList($event, index)"
                                :num="item.quantity"
                                :maxQuantity="item.skuStock" />
                        </view>
                    </view>
                    <view
                        v-if="activeIndex === index && goodsType == 2 && itemTransLateX"
                        @touchstart.stop
                        @touchmove.stop
                        @touchend.stop
                        class="del"
                        :style="{
                            top: 0,
                            right: itemTransLateX ? '-144rpx' : 0,
                            width: itemTransLateX ? '144rpx' : 0
                        }">
                        <view class="delicon" @touchmove.stop @touchend.stop @touchstart.stop="delGoods(item, index)">
                            <img class="iconimg" :src="getImgOssUrl('/ui-prod-2.0.0/delgoods_1.2.png')" />
                            <text class="icontext">删除</text>
                        </view>
                    </view>
                </view>
            </view>
            <!-- 下架、售罄、失效商品 -->
            <view class="carpage_goods bg" v-if="expireData.length && islogin">
                <view
                    v-for="(item, index) in expireData"
                    :key="index + item.id"
                    class="carpage_square"
                    @touchstart="itemTouchStart($event, index, 3)"
                    @touchend="itemTouchEnd($event, index, 3)"
                    @tap="goDetail(item)"
                    :style="{
                        transform:
                            activeIndex === index && goodsType == 3 && itemTransLateX
                                ? 'translateX( ' + itemTransLateX + 'rpx)'
                                : 0,
                        transition: 'all 0.4s'
                    }">
                    <div style="height: 200rpx; line-height: 200rpx" @touchstart.stop @touchmove.stop @touchend.stop>
                        <checkbox
                            value="item.checked"
                            :checked="item.checked"
                            style="transform: scale(0.7)"
                            color="#FE292B"
                            :disabled="
                                (item.activity && item.activity.activityType == 2) ||
                                [2, 3].includes(item.onSale) ||
                                !item.skuStock ||
                                item.isDelete == 1
                            "
                            class="checkbox round red" />
                    </div>
                    <view class="img_square">
                        <img
                            class="carpage_img"
                            :src="item.coverSrc || defaultImg"
                            :lazy-load="true"
                            @error="imageError(index)" />
                        <view class="img_logo" v-if="item.isDelete == 1"> 商品已失效 </view>
                        <view class="img_logo" v-else-if="item.onSale == 2"> 商品已下架 </view>
                        <view class="img_logo" v-else-if="item.skuStock == 0"> 商品已售罄 </view>
                    </view>
                    <view class="carpage_detail">
                        <view class="carpage_foodname">{{ item.goodsName }}</view>
                        <view
                            v-if="item.onSale != 2 && item.isDelete == 2"
                            class="carpage_stylechoose"
                            @touchmove.stop
                            @touchend.stop
                            @tap.stop="openDialog(item)">
                            <text class="carpage_text">{{ item.skuSpecValue }}</text>
                            <img class="carpage_jintou" :src="getImgOssUrl('/ui-prod-2.0.0/jiantou_1.2.png')" />
                        </view>
                        <view v-if="item.activity && item.activity.activityType == 2" class="activity_goods">
                            活动商品不支持购物车下单
                        </view>
                        <view v-else class="carpage_count">
                            <view class="carpage_color" v-if="item.onSale == 1 && item.isDelete == 2">
                                <text class="price-qian">￥</text>
                                <text class="carpage_money">
                                    {{
                                        floatDivide(item.skuPrice, 100).toString().indexOf('.') != -1
                                            ? floatDivide(item.skuPrice, 100).toString().split('.')[0]
                                            : floatDivide(item.skuPrice, 100)
                                    }}
                                </text>
                                <text
                                    class="price-dian"
                                    v-if="floatDivide(item.skuPrice, 100).toString().indexOf('.') != -1">
                                    {{ '.' + floatDivide(item.skuPrice, 100).toString().split('.')[1] }}
                                </text>
                            </view>

                            <ShopingGoodsSteps
                                @touchend.stop
                                v-if="item.onSale == 1 && item.skuStock && item.isDelete == 2"
                                @AddShoppingList="AddShoppingList($event, index)"
                                :num="item.quantity"
                                :maxQuantity="item.skuStock" />
                        </view>
                    </view>
                    <view
                        v-if="activeIndex === index && goodsType == 3 && itemTransLateX"
                        @touchstart.stop
                        @touchmove.stop
                        @touchend.stop
                        class="del"
                        :style="{
                            top: 0,
                            right: itemTransLateX ? '-144rpx' : 0,
                            width: itemTransLateX ? '144rpx' : 0
                        }">
                        <view class="delicon" @touchmove.stop @touchend.stop @touchstart.stop="delGoods(item, index)">
                            <img class="iconimg" :src="getImgOssUrl('/ui-prod-2.0.0/delgoods_1.2.png')" />
                            <text class="icontext">删除</text>
                        </view>
                    </view>
                </view>
            </view>

            <view class="search_no" v-if="!cartList.length && islogin && !expireData.length && !activityData.length">
                <img class="denglu" :src="getImgOssUrl('/ui-prod-2.0.0/denglu_2.0.png')" />
                <text class="denglu_title">您的购物车居然是空的</text>
                <text class="denglu_title">快去挑好货吧</text>
            </view>
            <view class="search_no" v-if="!islogin">
                <img class="denglu" :src="getImgOssUrl('/ui-prod-2.0.0/denglu_2.0.png')" />
                <text class="denglu_title">登录后可查看购物车商品</text>
                <button @click="router.push('/pages-user/login/index')" class="btn_denglu">去登录</button>
            </view>
        </view>
        <!-- 购物付款操作栏  -->
        <view class="tab" v-if="!show && islogin && (expireData.length || cartList.length || activityData.length)">
            <view class="carpage_bottom">
                <view>
                    <checkbox
                        :checked="operateParam.checked"
                        color="#FE292B"
                        class="round red"
                        @touchmove.stop
                        @touchend.stop
                        @click="allChoose"
                        style="transform: scale(0.7)" />
                    <text class="label space">全选</text>
                </view>
                <view class="carpage_bottom" style="padding: 0; height: auto">
                    <view>
                        <view class="carpage_bottom" style="padding: 0; height: auto">
                            <text class="label">总价</text>
                            <view class="carpage_color">
                                <text class="price-qian">￥</text>
                                <text class="carpage_allcount">{{
                                    operateParam.allMoney.toString().indexOf('.') != -1
                                        ? operateParam.allMoney.toString().split('.')[0]
                                        : operateParam.allMoney
                                }}</text>
                                <text class="price-dian" v-if="operateParam.allMoney.toString().indexOf('.') != -1">
                                    {{ '.' + operateParam.allMoney.toString().split('.')[1] }}
                                </text>
                            </view>
                        </view>
                        <text class="cheap_num" v-if="floatDivide(operateParam.cheapMoney, 100)">
                            已优惠&nbsp;&nbsp;￥{{ floatDivide(operateParam.cheapMoney, 100) }}
                        </text>
                    </view>
                    <view>
                        <button @click="toDetail" :class="[hasChoose ? 'btnshop' : 'btnshop disabled']">去结算</button>
                    </view>
                </view>
            </view>
        </view>
    </view>
    <uni-popup ref="popup" :safe-area="false" @change="change">
        <view class="popup-content popup_padding_b">
            <view class="pop_cancel" @click.stop="close">
                <img class="pop_logo" :src="getImgOssUrl('/ui-prod-2.0.0/cancel_1.1.png')" />
            </view>
            <skupop
                :goodsId="props.goodsId"
                :goodsNo="props.goodsNo"
                :specValueList="props.specValueList"
                :skuId="props.skuId"
                :coverSrc="props.coverSrc"
                :quantity="props.quantity"
                :cartId="props.cartId"
                :isvalid="props.isvalid"
                :show="show"
                @popup="popup.open('bottom')"
                @close="close"
                @updata="updata" />
        </view>
    </uni-popup>
    <el-loading></el-loading>
    <TabBar url="/pages/tabbar/cart" />
</template>
<script setup>
import SkeletonCard from '@/components/skeletonCard';
import TabBar from '@/components/tabBar';
import { ref, reactive, computed, nextTick } from 'vue';
import { onPullDownRefresh, onShow, onPageScroll } from '@dcloudio/uni-app';
import request from '@/request';
import router from '@/router';
import ShopingGoodsSteps from '@/components/shopingGoodsSteps';
import skupop from '@/components/skupop';
import { checkToken, floatDivide, getImgOssUrl, isNotEmpty } from '@/commons/utils';
import { useTrace } from '@/commons/trace';
import { getCorpInfo, getTabBarInfo } from '@/hooks';
const defaultImg = ref(getImgOssUrl('/ui-prod-2.0.0/errorImg.png'));
const { traceReport } = useTrace({ autoLoad: true });
const props = reactive({ goodsId: '', goodsNo: '', specValueList: {}, coverSrc: '' });
const skeletonLoading = ref(true);
let goodsType = ref(''); //1 满减商品 2 非满减上架商品 3下架、售罄、失效商品
let activeIndex = ref(''); //滑动商品的索引值
let startX = ref(0);
let startY = ref(0);
let itemTransLateY = ref(0);
let itemTransLateX = ref(0);
let show = ref(false);
let scrollTop = ref(0);
let islogin = ref(true);
const popup = ref();
let hasChoose = ref(false);
let total = ref(0);
let isscroll = ref(false); //判断是否有已经左滑的商品
let manjianIndex = ref(0); //满减活动的索引
/** 付款栏数据*/
const operateParam = reactive({ allMoney: 0, checked: false, cheapMoney: 0 });
// 购物车列表数据
const cartList = ref([]);
/**
 * 满减活动列表[[],[]]
 */
const activityData = ref([]);
/**满减详情 */
const saleDetail = ref([]);
// 下架、售罄、失效商品列表
const expireData = ref([]);
onShow(async () => {
    islogin.value = await checkToken();
    if (!islogin.value) {
        skeletonLoading.value = false;
        return;
    }
    uni.pageScrollTo({ scrollTop: 0 });
    init();
    getCartList();
});
const onInitData = async () => {
    uni.hideTabBar();
    await getCorpInfo();
    getTabBarInfo();
};
onInitData();

const init = () => {
    goodsType.value = '';
    activeIndex.value = '';
    operateParam.checked = false;
    hasChoose.value = false;
    operateParam.allMoney = 0;
    operateParam.cheapMoney = 0;
};
const goDetail = item => {
    if (!itemTransLateX.value && !isscroll.value) {
        // router.push('/pages-cart/good/index?goodsNo=' + item.goodsNo + '&goodsId=' + item.goodsId);
        let query = {
            goodsNo: item.goodsNo,
            goodsId: item.goodsId
        };
        router.push({
            path: '/pages-cart/good/index',
            query: query
        });
    }
};
const getCartList = () => {
    request('cartListServ')
        .then(res => {
            expireData.value = res?.data?.expireGoods;
            //获得下架、失效、售罄商品的已选规格值
            expireData.value.forEach((item, index) => {
                expireData.value[index] = { ...item, skuSpecValue: '' };
                expireData.value[index].skuSpecValue = item.specValueList.map(value => value.attributeValue).join(';');
            });
            //获得正常商品的已选规格值，并重置为未选中状态置false
            res.data.list.forEach((item, index) => {
                res.data.list[index] = { ...item, skuSpecValue: '', checked: false };
                res.data.list[index].skuSpecValue = item.specValueList.map(value => value.attributeValue).join(';');
            });
            cartList.value = res.data.list.filter(
                val =>
                    !val.activity ||
                    (val.activity?.activityType != 7 &&
                        val.activity?.activityStatusName.realStatus == 1 &&
                        val.activity?.activityType != 8)
            );
            total.value = res.data.totalCount;
            activityData.value = [];
            saleDetail.value = [];
            const hash = {};
            const activityInfo = res.data.activityInfo.reduce((item, next) => {
                hash[next.id] ? '' : (hash[next.id] = true && item.push(next));
                return item;
            }, []);
            /**
             * 获取满减活动数据列表
             * activityData.value:[[满减活动1],[满减活动2]....]
             * */
            activityInfo.forEach(item => {
                let data = res.data.list.filter(
                    val =>
                        item.id == val.activity?.id &&
                        ((val.activity?.activityType == 7 && val.activity?.activityStatusName.realStatus == 2) ||
                            (val.activity?.activityType == 8 && val.activity?.activityStatusName.realStatus == 2))
                );
                data.length ? activityData.value.push(data) : '';
            });
            /**
             * 获取满减活动详情数据
             * saleDetail.value:[{满减活动1},{满减活动2}....]
             * id：活动id，orderAmount（activityType为7 满几元 activityType为8 满几件），reduce（activityType为7 减几元 activityType为8 打几折），money：已凑的金额，select：满减活动是否有选中商品（true：已选中），isMax：是否达到活动满减金额（true：已达到），maxNum：选中的件数
             * */
            activityData.value.forEach(item => {
                let { orderAmount, reduce } = item[0].activity.discountSet;
                saleDetail.value.push({
                    id: item[0].activity.id,
                    orderAmount,
                    reduce,
                    money: '',
                    select: false,
                    isMax: false,
                    maxNum: 0
                });
            });
            skeletonLoading.value = false;
        })
        .catch(error => {
            skeletonLoading.value = false;
            uni.showToast({
                title: error.data.msg,
                icon: 'none',
                duration: 2000
            });
        });
};
// 下拉刷新
onPullDownRefresh(() => {
    updata();
    getTabBarInfo();
    setTimeout(function () {
        uni.stopPullDownRefresh();
    }, 1000);
});
const updata = () => {
    uni.pageScrollTo({ scrollTop: 0 });
    init();
    getCartList();
};

const openDialog = data => {
    props.specValueList = JSON.parse(JSON.stringify(data.specValueList));
    props.goodsId = data.goodsId;
    props.goodsNo = data.goodsNo;
    props.skuId = data.skuId;
    props.cartId = data.cartId;
    props.quantity = data.quantity;
    props.coverSrc = data.coverSrc;
    props.isvalid = data.onSale != 1 || !data.skuStock || data.isDelete == 1 ? false : true;
    // popup.value.open('bottom');
    show.value = true;
};
/** 判断购物车是否全选中*/
const getallcheckStatus = () => {
    const common = cartList.value.filter(item => item.checked == false);
    let manjian = [];
    activityData.value.forEach(item => {
        let flag = item.filter(item => item.checked == false);
        manjian = [...manjian, ...flag];
    });
    return common.length > 0 || manjian.length > 0 ? false : true;
};
const AddShoppingList = (type, index, indexs, param) => {
    if (type == 1) {
        if (
            param == 'manjian' &&
            activityData.value[indexs][index].quantity < activityData.value[indexs][index].skuStock
        ) {
            activityData.value[indexs][index].quantity++;
            updataCart(index, indexs, 'manjian');
        }
        if (!param && cartList.value[index].quantity < cartList.value[index].skuStock) {
            cartList.value[index].quantity++;
            updataCart(index);
        }
        if (cartList.value[index].quantity < cartList.value[index].skuStock) {
        }
    } else {
        if (!param && cartList.value[index].quantity > 1) {
            cartList.value[index].quantity--;
            updataCart(index);
        }
        if (param == 'manjian' && activityData.value[indexs][index].quantity > 1) {
            activityData.value[indexs][index].quantity--;
            updataCart(index, indexs, 'manjian');
        }
    }
};
/** 修改购物车*/
const updataCart = (index, indexs, param) => {
    if (param == 'manjian') {
        const { goodsId, quantity, skuId, cartId } = activityData.value[indexs][index];
        request('cartUpdateServ', { goodsId, quantity, skuId, cartId }).then(res => {
            getPrice();
        });
    } else {
        const { goodsId, quantity, skuId, cartId } = cartList.value[index];
        request('cartUpdateServ', { goodsId, quantity, skuId, cartId }).then(res => {
            getPrice();
        });
    }
};
/** 获取选中的cartid*/
const getcartIds = () => {
    let manjian = [];
    let common = cartList.value.filter(item => item.checked).map(item => item.cartId);
    activityData.value.forEach(item => {
        let flag = item.filter(item => item.checked);
        manjian = [...manjian, ...flag];
    });
    manjian = manjian.map(item => item.cartId);
    return [...manjian, ...common];
};
/**获得满减选中商品优惠详情*/
const setMoney = () => {
    activityData.value.forEach((item, index) => {
        let flag = item.filter(item => item.checked);
        let money = 0;
        saleDetail.value[index].select = flag.length ? true : false;
        saleDetail.value[index].maxNum = 0;
        flag.map(item => {
            money += item.skuPrice * item.quantity;
            saleDetail.value[index].maxNum += item.quantity;
        });
        if (money >= saleDetail.value[index].orderAmount) {
            saleDetail.value[index].isMax = true;
        } else {
            saleDetail.value[index].isMax = false;
        }
        saleDetail.value[index].money = money;
    });
};
/** 全选操作*/
const allChoose = () => {
    operateParam.checked = !operateParam.checked;
    cartList.value.forEach(item => {
        if (item.activity) {
            if (item.activity.activityType != 2 && item.onSale == 1 && item.skuStock && item.isDelete == 2) {
                item.checked = operateParam.checked;
            }
        } else {
            if (item.onSale == 1 && item.skuStock && item.isDelete == 2) {
                item.checked = operateParam.checked;
            }
        }
    });
    activityData.value.forEach(item => {
        item.forEach(val => {
            if (val.onSale == 1 && val.skuStock && val.isDelete == 2) {
                val.checked = operateParam.checked;
            }
        });
    });
    getPrice();
};
const choose = (index, indexs, param) => {
    if (param == 'manjian') {
        if (
            [2, 3].includes(activityData.value[indexs][index].onSale) ||
            !activityData.value[indexs][index].skuStock ||
            activityData.value[indexs][index].isDelete == 1
        )
            return;
        activityData.value[indexs][index].checked = !activityData.value[indexs][index].checked;
    } else {
        if (
            (cartList.value[index].activity && cartList.value[index].activity.activityType == 2) ||
            [2, 3].includes(cartList.value[index].onSale) ||
            !cartList.value[index].skuStock ||
            cartList.value[index].isDelete == 1
        )
            return;
        cartList.value[index].checked = !cartList.value[index].checked;
    }
    // pageCart.value[index].checked = !pageCart.value[index].checked;
    operateParam.checked = getallcheckStatus();
    getPrice();
};
/**获取总价 */
const getPrice = () => {
    const cartIds = getcartIds();
    setMoney();
    if (cartIds.length) {
        hasChoose.value = true;
        request('getPriceServ', { cartIds }).then(res => {
            operateParam.allMoney = res.data.price / 100;
            operateParam.cheapMoney = res.data.discountPrice;
        });
    } else {
        hasChoose.value = false;
        operateParam.cheapMoney = 0;
        operateParam.allMoney = 0;
    }
};
const itemTouchStart = (e, i, type, index) => {
    // 获取满减活动的索引
    manjianIndex.value = index ? index : 0;
    activeIndex.value = i;
    goodsType.value = type;
    startX.value = e.changedTouches[0].pageX;
    startY.value = e.changedTouches[0].pageY;
    if (!itemTransLateX.value) {
        isscroll.value = false;
    } else {
        isscroll.value = true;
    }
};
const itemTouchEnd = (e, i) => {
    let endX = e.changedTouches[0].pageX;
    let endY = e.changedTouches[0].pageY;
    itemTransLateY.value = endY - startY.value;
    itemTransLateX.value = endX - startX.value;
    //避免上下滑动触发向左滑动商品
    if (itemTransLateY.value < 20 && itemTransLateY.value > -20) {
        itemTransLateX.value = itemTransLateX.value <= -26 ? -120 : 0;
    } else {
        itemTransLateX.value = itemTransLateX.value <= -26 ? -120 : 0;
    }
    if (!itemTransLateX.value && !isscroll.value) {
        isscroll.value = false;
    } else {
        isscroll.value = true;
    }
};
/**删除购物车 */
const delGoods = item => {
    const { goodsId, skuId, cartId } = item;
    request('cartdelServ', { goodsId, skuId, cartId }).then(res => {
        updata();
        uni.showToast({
            title: '删除成功！',
            icon: 'none'
        });
    });
};

const toDetail = () => {
    if (!hasChoose.value) return;
    nextTick(() => {
        getPayParam();
    });
};
const getPayParam = data => {
    let skuList = [];
    let cardIds = [];
    var param = [];
    cartList.value.map(el => {
        if (el.checked) {
            cardIds.push(el.cartId);
            skuList.push({
                goodsId: el.goodsId,
                quantity: el.quantity,
                skuId: el.skuId
            });
            param.push({
                productId: el.goodsId,
                num: el.quantity,
                skuId: el.skuId,
                skuName: el.skuSpecValue,
                productName: el.goodsName
            });
        }
    });
    activityData.value.forEach(item => {
        item.map(el => {
            if (el.checked) {
                cardIds.push(el.cartId);
                skuList.push({
                    goodsId: el.goodsId,
                    quantity: el.quantity,
                    skuId: el.skuId
                });
                param.push({
                    productId: el.goodsId,
                    num: el.quantity,
                    skuId: el.skuId,
                    skuName: el.skuSpecValue,
                    productName: el.goodsName
                });
            }
        });
    });
    traceReport('place_order_cart', param);
    router.push({
        path: '/pages-cart/confirm/index',
        query: {
            type: 2,
            skuList: JSON.stringify(skuList),
            cardIds: JSON.stringify(cardIds),
            orderType: 1
        }
    });
};
const close = () => {
    popup.value.close();
    show.value = false;
};
const change = type => {
    show.value = type.show;
};
const isBottom = computed(() => {
    return scrollTop.value > 100;
});
onPageScroll(e => {
    scrollTop.value = e.scrollTop;
});
const imageError = index => {
    cartList.value[index].coverSrc = defaultImg.value;
};
</script>
<style lang="scss" scoped>
.scroll {
    height: calc(100vh - 90rpx);
}
.btn_denglu {
    width: 192rpx;
    height: 65rpx;
    background: linear-gradient(270deg, #ff5356 0%, #fe292b 100%);
    border-radius: 40rpx;
    margin: 42rpx 0 0 0;
    width: 87px;
    font-size: 29rpx;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #ffffff;
    line-height: 65rpx;
}
.btnshop {
    height: 66rpx;
    margin-left: 16rpx;
    background: linear-gradient(270deg, #ff5356 0%, #fe292b 100%);
    border-radius: 36rpx;
    line-height: 66rpx;
    color: #ffffff;
    font-size: 25rpx;
    font-family: PingFangSC-Medium, PingFang SC;
    width: 180rpx;
}
.disabled {
    color: #ffff !important;
    background: #cccccc !important;
}
.carpage {
    margin-top: 18rpx;
    margin-bottom: 110rpx;

    .bg {
        background-color: #ffff;
    }
    .search_no {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .denglu {
            width: 297rpx;
            height: 216rpx;
            margin: 312rpx 0 42rpx 0;
        }
        .denglu_title {
            font-size: 29rpx;
            font-weight: 400;
            color: #333333;
            line-height: 40rpx;
        }
        .no_img {
            width: 278rpx;
            height: 160rpx;
            margin: 312rpx 0 24rpx 0;
        }
        .no_title {
            font-size: 24rpx;
            font-weight: 400;
            color: #333333;
            line-height: 34rpx;
        }
    }
    .carpage_goods {
        margin-bottom: 18rpx;
        padding: 0 28rpx;
        overflow-x: hidden;
        .carpage_head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: 72rpx;
            border-bottom: 2rpx solid #f5f5f5;
            .carpage_left {
                display: flex;
                align-items: center;
            }
            .logo {
                width: 55rpx;
                height: 29rpx;
                margin-right: 6rpx;
                padding: 4rpx;
                background: #fe292b;
                text-align: center;
                border-radius: 4rpx;
                font-size: 22rpx;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #ffffff;
                line-height: 29rpx;
            }
            .saletips {
                font-size: 25rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                .detailPhoto {
                    width: 11rpx;
                    height: 18rpx;
                    margin-left: 8rpx;
                }
            }
            .color {
                color: #fe292b;
            }
        }
        .carpage_square {
            display: flex;
            padding: 28rpx 0;
            border-bottom: 2rpx solid #f5f5f5;
            &:last-child {
                border: none;
            }

            .img_square {
                position: relative;
                width: 200rpx;
                height: 200rpx;
                left: 0;
                bottom: 0;
                margin-left: 16rpx;
                border-radius: 7rpx;
                flex-shrink: 0;
            }
            .img_logo {
                position: absolute;
                bottom: 0;
                width: 100%;
                height: 43rpx;
                font-size: 22rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #ffffff;
                line-height: 43rpx;
                text-align: center;
                border-radius: 0 0 7rpx 7rpx;
                background: rgba(51, 51, 51, 0.4);
            }
            .del {
                background-color: #fe292b;
                position: absolute;
                right: 0;
                height: 100%;
            }
            .delicon {
                display: flex;
                flex-direction: column;
                align-items: center;
                height: 100%;
                justify-content: center;
                .iconimg {
                    width: 40rpx;
                    height: 40rpx;
                }
                .icontext {
                    margin-top: 14rpx;
                    font-size: 26rpx;
                    font-weight: 500;
                    color: #ffffff;
                    line-height: 26rpx;
                }
            }
            .carpage_img {
                width: 100%;
                height: 100%;
                border-radius: 8rpx;
                flex-shrink: 0;
            }
            .carpage_detail {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                margin-left: 18rpx;
                padding-right: 28rpx;
                width: 100%;
                .carpage_foodname {
                    font-size: 26rpx;
                    font-weight: 500;
                    color: #33302d;
                    font-family: PingFangSC-Medium, PingFang SC;
                    line-height: 36rpx;
                    display: -webkit-box;
                    word-break: break-all;
                    height: 72rpx;
                    overflow: hidden;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                }

                .carpage_stylechoose {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    height: 44rpx;
                    width: 162rpx;
                    background-color: #f5f5f5;
                    margin-top: 10rpx;
                    padding: 6rpx 16rpx;
                    border-radius: 8rpx;
                    .carpage_text {
                        width: 78%;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        font-size: 24rpx;
                        font-weight: 400;
                        color: #999999;
                        line-height: 32rpx;
                    }
                    .carpage_jintou {
                        width: 24rpx;
                        height: 14rpx;
                    }
                }
                .carpage_count {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-top: 18rpx;
                    .carpage_color {
                        color: #fe292b;
                        font-weight: 500;
                        font-family: PingFangSC-Medium, PingFang SC;
                    }
                    .price-qian {
                        font-size: 24rpx;
                    }
                    .price-dian {
                        font-size: 26rpx;
                    }
                    .carpage_money {
                        font-size: 36rpx;
                        line-height: 40rpx;
                    }
                }
                .activity_goods {
                    font-size: 28rpx;
                    font-weight: 400;
                    color: #999999;
                }
            }
        }
    }
}
.tab {
    width: 100%;
    height: 100rpx;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    padding-bottom: calc(50px + constant(safe-area-inset-bottom));
    padding-bottom: calc(50px + env(safe-area-inset-bottom));
    box-shadow: 0px -2px 4px 0px rgba(153, 153, 153, 0.08);
    .carpage_bottom {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 100rpx;
        padding: 0 28rpx;
        .cheap_num {
            font-size: 22rpx;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #fe292b;
            line-height: 25rpx;
        }
        .label {
            font-size: 25rpx;
            font-weight: 400;
            font-family: PingFangSC-Regular, PingFang SC;
            color: #333333;
            line-height: 25rpx;
        }
        .space {
            padding-left: 16rpx;
        }
        .carpage_color {
            color: #fe292b;
            font-weight: 600;
            font-family: PingFangSC-Medium, PingFang SC;
            padding-left: 10rpx;
        }
        .price-qian {
            font-size: 24rpx;
        }
        .price-dian {
            font-size: 26rpx;
        }
        .carpage_allcount {
            font-size: 36rpx;
            line-height: 36rpx;
        }
    }
}
</style>
